package cn.chiship.framework.upms.biz.user.service.impl;

import cn.chiship.framework.upms.biz.user.entity.*;
import cn.chiship.framework.upms.biz.user.mapper.UpmsPermissionApiMapper;
import cn.chiship.framework.upms.biz.user.pojo.vo.UpmsUserOrganizationVo;
import cn.chiship.framework.upms.biz.user.pojo.vo.UpmsUserVo;
import cn.chiship.framework.upms.biz.user.service.UpmsUserOrganizationService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.upms.biz.user.mapper.UpmsUserOrganizationMapper;

import javax.annotation.Resource;

import cn.chiship.sdk.framework.pojo.vo.PageVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 用户组织关联表业务接口实现层
 * 2021/9/27
 *
 * @author lijian
 */
@Service
public class UpmsUserOrganizationServiceImpl extends BaseServiceImpl<UpmsUserOrganization, UpmsUserOrganizationExample> implements UpmsUserOrganizationService {

    @Resource
    UpmsUserOrganizationMapper upmsUserOrganizationMapper;
    @Resource
    UpmsPermissionApiMapper upmsPermissionApiMapper;

    /**
     * 用户和组织分页
     *
     * @param page
     * @param limit
     * @param paramsMap
     * @return
     */
    @Override
    public BaseResult pageUserByOrgId(Long page, Long limit, String orgId, HashMap<String, Object> paramsMap) {
        paramsMap.put("orgId", orgId);
        Byte includeOrgs = (Byte) paramsMap.get("includeOrgs");
        Page pageResult = PageHelper.startPage(page.intValue(), limit.intValue());
        List<UpmsUserOrganizationVo> users = upmsUserOrganizationMapper.selectUserAndOrg(paramsMap);
        List<JSONObject> records = new ArrayList<>();
        users.forEach(user -> {
            JSONObject json = JSON.parseObject(JSONObject.toJSONString(user));
            records.add(json);
        });
        if (Byte.valueOf("1").equals(includeOrgs)) {
            for (JSONObject user : records) {
                user.put("upmsOrganizations", upmsPermissionApiMapper.selectOrganizationUserByUserId(user.getString("id")));
            }
        }
        PageVo pageVo = new PageVo();
        pageVo.setRecords(records);
        pageVo.setTotal(pageResult.getTotal());
        pageVo.setPages(Long.valueOf(pageResult.getPages()));
        pageVo.setCurrent(page);
        pageVo.setSize(limit);
        return BaseResult.ok(pageVo);
    }

    @Override
    public BaseResult selectDetailsByPrimaryKey(Object id) {
        HashMap<String, Object> paramsMap = new HashMap();
        paramsMap.put("userOrgId", id);
        List<UpmsUserOrganizationVo> userVos = upmsUserOrganizationMapper.selectUserAndOrg(paramsMap);
        if (userVos.isEmpty()) {
            return BaseResult.ok();
        }
        UpmsUserOrganizationVo userVo = userVos.get(0);
        JSONObject json = JSON.parseObject(JSONObject.toJSONString(userVo));
        json.put("upmsRoles", upmsPermissionApiMapper.selectRoleByUserId(userVo.getId()));
        return BaseResult.ok(json);
    }

    @Override
    public BaseResult insertSelective(UpmsUserOrganization userOrganization) {
        UpmsUserOrganizationExample userOrganizationExample = new UpmsUserOrganizationExample();
        userOrganizationExample.createCriteria()
                .andUserIdEqualTo(userOrganization.getUserId())
                .andOrganizationIdEqualTo(userOrganization.getOrganizationId());

        if (upmsUserOrganizationMapper.countByExample(userOrganizationExample) > 0) {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "组织已存在选择的用户，请重新选择");
        }
        return super.insertSelective(userOrganization);
    }

    @Override
    public BaseResult validateExist(String userId, String orgId) {
        UpmsUserOrganizationExample userOrganizationExample = new UpmsUserOrganizationExample();
        userOrganizationExample.createCriteria()
                .andUserIdEqualTo(userId)
                .andOrganizationIdEqualTo(orgId);

        if (upmsUserOrganizationMapper.countByExample(userOrganizationExample) > 0) {
            return BaseResult.ok(true);
        }
        return BaseResult.ok(false);
    }
}
